** Data reading and variable selection from raw data
** Korea Inequality and Equity Survey 1990


** 01. Reading data **

cap log close
clear all
set more off
cd /*insert you work directory here*/
use /*read your data here*/  
numlabel, add


** 02. Consructing year and country variables **

ge year=1990
lab var year "survey year"

ge country=410
lab var country "ISO country code"
//south korea: 410 (ISO Country Codes) 


** 03. ID variables **

ge hid=id1
lab var hid "household id"

ge pid=_n
lab var pid "person id"


** 04. Basic Demographics (Sex and Age/birth year) **

ge sex=q1
lab var sex "sex"
lab def sex 1 "male" 2 "female"
lab val sex sex

ge age=q2
lab var age "age"

ge birthyr=year-age
lab var birthyr "year of birth"


** 05. Siblings **

ge nbro=q19_1-1 if sex==1  
replace nbro=q19_1 if sex==2

ge nsis=q19_2 if sex==1
replace nsis=q19_2-1 if sex==2
* q19_1 and q19_2 include respondent

ge nsibs=nbro+nsis

ge birthorder=q19_3

lab var nbro "number of brothers"
lab var nsis "number of sisters"
lab var nsibs "number of siblings"
lab var birthorder "birth order"


** 06. Own education **

ge educ=q23_1
lab var educ "highest level of education completed"
lab def educ 1 "no schooling" 2 "primary" 3 "lower secondary" 4 "upper secondary" 5 "junior college" 6 "4-year college" 7 "postgraduate" 9 "no response"
lab val educ educ


** 07. Parents' education: Father and/or Mother **

ge faeduc=q22_1
ge maeduc=q22_2
lab var faeduc "father's education"
lab var maeduc "mother's education"
lab val faeduc maeduc educ


** 08. Own occupation **

ge firstocc=q28_2
ge firstempstat=q28_3
lab var firstocc "first occupation"
lab var firstempstat "employment status of first occupation"

ge occ=q30_2
ge empstat=q30_3
lab var occ "current occupation"
lab var empstat "employment status of current occupation"

lab def empstat 1 "employer" 2 "self-employed" 3 "family worker" 4 "permanent employee" 5 "temporary employee" 9 "don't know"
lab val firstempstat empstat empstat


** 09. Parents' occupation **

ge faocc=q26_1
lab var faocc "father's occupation when young"


** 10. Tabulate the Identified Variables **

log using /*insert you work directory here*/, replace text

** Data reading and variable selection from raw data
** Korea Inequality and Equity Survey 1990

** Sex **
tab sex

** Age, Birth Year **
sum age birthyr, d

** Siblings **
sum nsibs nbro nsis birthorder, d

** R's Own Education **
tab1 educ 

** Parental Education **
tab1 faeduc maeduc 

** R's Own Occupation **
tab1 occ empstat 

** Parental Occupation **
tab1 faocc 

log close

** 11. Keep the identified variables only

keep year country hid pid sex age birthyr ///
	 nbro nsis nsibs birthorder ///
	 educ faeduc maeduc ///
	 firstocc firstempstat occ empstat faocc


** 13. Create educational years variable **

*rename educational level variable to educ_cat*
rename educ educ_cat 
rename faeduc faeduc_cat 
rename maeduc moeduc_cat 

ge educ_yrs = .
replace educ_yrs = 0 if educ_cat == 1
replace educ_yrs = 6 if educ_cat == 2
replace educ_yrs = 8 if educ_cat == 3
replace educ_yrs = 12 if educ_cat == 4
replace educ_yrs = 14 if educ_cat == 5
replace educ_yrs = 16 if educ_cat == 6
replace educ_yrs = 18 if educ_cat == 7
replace educ_yrs = . if educ_cat == 9

lab var educ_cat "Respondent's years of education"

ge faeduc_yrs = .
replace faeduc_yrs = 0 if faeduc_cat == 1
replace faeduc_yrs = 6 if faeduc_cat == 2
replace faeduc_yrs = 8 if faeduc_cat == 3
replace faeduc_yrs = 12 if faeduc_cat == 4
replace faeduc_yrs = 14 if faeduc_cat == 5
replace faeduc_yrs = 16 if faeduc_cat == 6
replace faeduc_yrs = 18 if faeduc_cat == 7
replace faeduc_yrs = . if faeduc_cat == 9

lab var faeduc_cat "Father's years of education"

ge moeduc_yrs = .
replace moeduc_yrs = 0 if moeduc_cat == 1
replace moeduc_yrs = 6 if moeduc_cat == 2
replace moeduc_yrs = 8 if moeduc_cat == 3
replace moeduc_yrs = 12 if moeduc_cat == 4
replace moeduc_yrs = 14 if moeduc_cat == 5
replace moeduc_yrs = 16 if moeduc_cat == 6
replace moeduc_yrs = 18 if moeduc_cat == 7
replace moeduc_yrs = . if moeduc_cat == 9

lab var moeduc_cat "Mother's years of education"

** 12. Save the Data File **

saveold  /*insert you work directory here*/, replace

